/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.unicen.twitter.suggest.backend.datasources.dao;

import ar.edu.unicen.twitter.suggest.backend.dtos.UsuarioApi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Alex
 */
public class CacheFIFO<K,V> {
    
    int maxSize;
    private Map<K, V> cache;
    private List<K> queue;
    
    public CacheFIFO(int maxSize){
        this.maxSize = maxSize;
        cache = new HashMap<K, V>(maxSize);
        queue = new ArrayList<K>(maxSize);
    }
    
    public void clear(){
        cache.clear();
        queue.clear();
    }
    
    public void put(K key, V value)
    {
        if(cache.containsKey(key))
        {
            //queue.remove(key);
            //queue.add(key);
            cache.put(key, value); 
            
        }
        else
        {
            while(queue.size()>= maxSize)
            {
                K removedKey = queue.remove(0);
                cache.remove(removedKey);
            }
            queue.add(key);
            cache.put(key, value); 
        }
    }
    public V get(K key)
    {
        return cache.get(key);
    }        
}
